package com.study.hot.jetpack.demo_words.ui;

import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.study.hot.R;
import com.study.hot.jetpack.demo_words.db.Words;
import com.study.hot.jetpack.demo_words.viewmodel.WordsViewModel;

import java.util.List;

/**
 * <pre>
 *     author : slx
 *     time   : 2020/03/31
 *     desc   :
 * </pre>
 */
public class WordsAdapter extends RecyclerView.Adapter<WordsAdapter.WordHolder> {

    private List<Words> wordsList;
    private WordsViewModel wordsViewModel;
    private boolean isCard;

    public WordsAdapter(WordsViewModel wordsViewModel, boolean isCard) {
        this.wordsViewModel = wordsViewModel;
        this.isCard = isCard;
    }

    public void setWordsList(List<Words> wordsList) {
        this.wordsList = wordsList;
    }

    @NonNull @Override public WordHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        int layoutId = isCard ? R.layout.item_words_card : R.layout.item_words;
        WordHolder holder = new WordHolder(LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false));
        holder.itemView.setOnClickListener(v -> {
            Uri uri = Uri.parse("https://m.youdao.com/dict?le=eng&q=" + holder.tvEnglish.getText());
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(uri);
            holder.itemView.getContext().startActivity(intent);
        });
        holder.switchBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            Words words = (Words) holder.itemView.getTag(R.id.words_for_view_holder);
            holder.tvChinese.setVisibility(isChecked ? View.VISIBLE : View.GONE);
            words.setShowChinese(isChecked);
            //写入数据库
            wordsViewModel.updateWords(words);
        });
        return holder;
    }

    @Override public void onBindViewHolder(@NonNull WordHolder holder, int position) {
        Words words = wordsList.get(position);
        holder.itemView.setTag(R.id.words_for_view_holder, words);
        holder.tvId.setText(String.valueOf(words.getWordsId()));
        holder.tvEnglish.setText(words.getWordsEnglish());
        boolean showChinese = words.isShowChinese();
        if (showChinese) {
            holder.tvChinese.setText(words.getWordsChinese());
            holder.tvChinese.setVisibility(View.VISIBLE);
            holder.switchBtn.setChecked(true);
        } else {
            holder.tvChinese.setVisibility(View.GONE);
            holder.switchBtn.setChecked(false);
        }
        //item每次复用都绑定listener消耗性能，可以在onCreateViewHolder中绑定监听
        //holder.switchBtn.setOnCheckedChangeListener(null);
        /*holder.itemView.setOnClickListener(v -> {
            Uri uri = Uri.parse("https://m.youdao.com/dict?le=eng&q=" + words.getWordsEnglish());
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(uri);
            holder.itemView.getContext().startActivity(intent);
        });*/
        /*holder.switchBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            holder.tvChinese.setVisibility(isChecked ? View.VISIBLE : View.GONE);
            words.setShowChinese(isChecked);
            //写入数据库
            wordsViewModel.updateWords(words);
        });*/
    }

    @Override public int getItemCount() {
        return wordsList == null ? 0 : wordsList.size();
    }

    static class WordHolder extends RecyclerView.ViewHolder {
        TextView tvId;
        TextView tvChinese;
        TextView tvEnglish;
        Switch switchBtn;

        WordHolder(@NonNull View itemView) {
            super(itemView);
            tvId = itemView.findViewById(R.id.id);
            tvChinese = itemView.findViewById(R.id.chinese);
            tvEnglish = itemView.findViewById(R.id.english);
            switchBtn = itemView.findViewById(R.id.switchBtn);
        }
    }
}
